/**
 * @fileOverview 微信小程序的入口文件
 */

var qcloud = require('./vendor/qcloud-weapp-client-sdk/index');
var config = require('./config');
var Session = require('./vendor/qcloud-weapp-client-sdk/lib/session');

// 显示繁忙提示
var showBusy = text => wx.showToast({
    title: text,
    icon: 'loading',
    duration: 10000
});

// 显示成功提示
var showSuccess = text => {
    wx.hideToast();
    wx.showToast({
        title: text,
        icon: 'success'
    });
};

// 显示失败提示
var showModel = (title, content) => {
    wx.hideToast();

    wx.showModal({
        title,
        content: content,
        showCancel: false
    });
};
App({
    /**
     * 小程序初始化时执行，我们初始化客户端的登录地址，以支持所有的会话操作
     */
    onLaunch(options) {
        console.log(options);
        qcloud.setLoginUrl(config.service.loginUrl);
        // this.doLogin();
    },
    getUserInfo(cb) {
        var that = this
        var session=Session.get();
        if (session && session.id && session.skey) {
            qcloud.request({
                // 要请求的地址
                url: config.service.requestUrl,
                success(result) {
                    typeof cb == "function" && cb(result.data.data.userInfo);
                    console.log('request success', result);
                },

                fail(error) {
                    console.log('request fail', error);
                },

                complete() {
                    console.log('request complete');
                }
            });
        }else{
            qcloud.login({
                success(result) {
                    showSuccess('登录成功');
                    console.log('登录成功', result);
                    qcloud.request({
                        // 要请求的地址
                        url: config.service.requestUrl,
                        success(result) {
                            typeof cb == "function" && cb(result.data.data.userInfo);
                            console.log('request success', result);
                        },

                        fail(error) {
                            console.log('request fail', error);
                        },

                        complete() {
                            console.log('request complete');
                        }
                    });
                },

                fail(error) {
                    showModel('错误提示', '需要微信授权才能体验，稍后请重试');
                    console.log('登录失败', error);
                }
            });
        }
        
  }
});